<?php

namespace app\common\model;

use think\Model;
use app\api\model\xykeep\User;
use think\Request;

class Benfit extends Model
{

    

    

    // 表名
    protected $name = 'benfit';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'content'
    ];
    

    
    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function card()
    {
        return $this->belongsTo('Card', 'card_id')->setEagerlyType(1);
    }

    public function supplier()
    {
        return $this->belongsTo('User', 'supplier_id')->setEagerlyType(1);
    }

    public function goods()
    {
        return $this->belongsTo('app\admin\model\xykeep\Goods', 'goods_id')->setEagerlyType(1);
    }

    //是否被购买
    public function buyed()
    {
        $user = User::info();
        if($user){
            $user_id = $user->id;
        }else{
            $user_id = 0;
        }
        return $this->belongsTo('app\common\model\UserCardBenfit', 'id','benfit_id')->bind('nums')->where([
            'user_id' => $user_id
        ])->setEagerlyType(1);
    }

    public function getContentAttr($value, $data)
    {
        $content = $data['content'];
        $pattern = '/(http|https)\:\/\/([a-z]+\.){1,3}[a-z]+/';
        $request = Request::instance();
        $domain = $request->domain();
        $content = preg_replace($pattern,$domain,$content);
        return $content;
    }
}
